/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[BorderComponent.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.components;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;

/**
 * Swing边界组件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class BorderComponent {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				BorderFrame frame = new BorderFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class BorderFrame extends JFrame {

	private static final long serialVersionUID = -8572302190121830960L;

	private static final int DEFAULT_WIDTH = 600;
	private static final int DEFAULT_HEIGHT = 200;

	private JPanel borderPanel = null;
	private JPanel buttonPanel = null;
	private ButtonGroup buttonGroup = null;

	public BorderFrame() {
		setTitle("Border Test");
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLocationByPlatform(true);
		setResizable(false);

		borderPanel = new JPanel();
		buttonPanel = new JPanel();
		buttonGroup = new ButtonGroup();

		// 绘制边框
		generateRadioButton("Empty", BorderFactory.createEmptyBorder());
		generateRadioButton("Lowerd Bevel", BorderFactory.createLoweredBevelBorder()); // 凹斜面
		generateRadioButton("Raised Bevel", BorderFactory.createRaisedBevelBorder()); // 凸斜面
		generateRadioButton("Etched", BorderFactory.createEtchedBorder());// 蚀刻
		generateRadioButton("Line", BorderFactory.createLineBorder(Color.BLUE));// 直线
		generateRadioButton("Matte", BorderFactory.createMatteBorder(10, 10, 10, 10, Color.RED));// 不光滑

		Border etched = BorderFactory.createEtchedBorder();
		Border titled = BorderFactory.createTitledBorder(etched, "Border Types");
		buttonPanel.setBorder(titled); // 设置border

		setLayout(new GridLayout(2, 1));
		getContentPane().add(buttonPanel);
		getContentPane().add(borderPanel);
	}

	/**
	 * 添加单选按钮
	 * 
	 * @param radioName
	 *            radio名称
	 * @param border
	 *            border名称
	 */
	public void generateRadioButton(String radioName, final Border border) {
		JRadioButton radioButton = new JRadioButton(radioName);
		if (radioName.equals("Empty")) {
			radioButton.setSelected(true);
		}
		radioButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				borderPanel.setBorder(border);// 设置border
			}
		});
		buttonGroup.add(radioButton);
		buttonPanel.add(radioButton);
	}
}
